package com.thinkingstar.iads.projectcenter.controller;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.thinkingstar.iads.datacenter.entity.DcProjectDesignCell;
import com.thinkingstar.iads.datacenter.entity.dao.DaoDcProjectDesignCell;
import com.thinkingstar.iads.datacenter.entity.dao.DaoDcProjectMention;
import com.thinkingstar.iads.projectcenter.entity.PcProjectInfo;
import com.thinkingstar.iads.projectcenter.entity.dao.DaoPcProjectData;
import com.thinkingstar.iads.projectcenter.serviceImpl.ProjectDcsjServiceImpl;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.thinkingstar.iads.common.filter.GlobalsFilter;
import com.thinkingstar.iads.common.filter.UserLoginFilter;
import com.thinkingstar.iads.common.tree.vo.CommonTreeVO;
import com.thinkingstar.iads.common.tree.vo.DesignCellDataTreeVO;
import com.thinkingstar.iads.common.tree.vo.DesignMajorDataTreeVO;
import com.thinkingstar.iads.common.tree.vo.DesignMentionDataTreeVO;
import com.thinkingstar.iads.common.utils.CodingFactory;
import com.thinkingstar.iads.cs.sys.enumerate.PROJECT_DESIGN_COURSE;
import com.thinkingstar.iads.cs.sys.enumerate.PROJECT_DESIGN_TYPE;
import com.thinkingstar.iads.datacenter.entity.DcProjectMentionInformation;
import com.thinkingstar.iads.projectcenter.serviceImpl.ProjectDesignManagerServiceImpl;

/**
 * 项目中心项目设计相关树
 * @author Administrator
 *
 */
@IocBean
@At("/projectCenter/treeapi")
@Filters({ @By(type = GlobalsFilter.class) , @By(type = UserLoginFilter.class)})
public class ProjectDesignTreeController {

	private static Log log = Logs.get();

	@Inject
	private ProjectDesignManagerServiceImpl projectDesignManagerServiceImpl;

	@Inject
	private ProjectDcsjServiceImpl projectDcsjServiceImpl;
	
	@Inject
	private DaoPcProjectData daoPcProjectData;
	
	@Inject
	private DaoDcProjectMention daoDcProjectMention;

	/**
	 * 根据项目id获取项目设计树
	 * @param project_id
	 * @return
	 */
	@At("/getProjectDesignByPid/?")
	@Ok("json")
	public Object getMajorTreeListByPid(String project_id , HttpServletRequest request){
		PcProjectInfo pcProjectInfo = daoPcProjectData.fetch(project_id);
		List <CommonTreeVO> data_list = new LinkedList<>();
		List <PROJECT_DESIGN_COURSE> design_list = PROJECT_DESIGN_COURSE.getAll(pcProjectInfo.getProject_design_content_name());
		for(int i=0;i<design_list.size();i++){
			PROJECT_DESIGN_COURSE design_obj = design_list.get(i);
			CommonTreeVO commonTreeVO = new CommonTreeVO(design_obj);
			if(PROJECT_DESIGN_COURSE.提入资料.getCode().equals(design_obj.getCode())){
				List <NutMap> major_list = projectDesignManagerServiceImpl.getCorrelationMajorByPid(project_id);
				List <CommonTreeVO> major_vo_list = new LinkedList<>();
				if(major_list != null && !major_list.isEmpty()){
					commonTreeVO.setIsParent(true);
					for(NutMap major_map : major_list){
						CommonTreeVO major_vo = new DesignMajorDataTreeVO(major_map,project_id);
						if(!PROJECT_DESIGN_COURSE.动车机务设施.getDesc().equals(major_vo.getName())){
							major_vo_list.add(major_vo);
						}
					}
					commonTreeVO.setChildren(major_vo_list);
				}
			}else if (PROJECT_DESIGN_COURSE.动车设计.getCode().equals(design_obj.getCode())){
				List <CommonTreeVO> major_vo_list = new LinkedList<>();
				List<DcProjectDesignCell> dcProjectDesignCellList = projectDcsjServiceImpl.getDesignCellDataListByPid(project_id);
				List <CommonTreeVO> designcell_data_list = new LinkedList<>();
				if(dcProjectDesignCellList != null && !dcProjectDesignCellList.isEmpty()){
					commonTreeVO.setIsParent(true);
					for(DcProjectDesignCell dcProjectDesignCell1 : dcProjectDesignCellList){
						DesignCellDataTreeVO mention_data = new DesignCellDataTreeVO(dcProjectDesignCell1,project_id);
						designcell_data_list.add(mention_data);
					}
					commonTreeVO.setChildren(designcell_data_list);
				}
			} 
			data_list.add(commonTreeVO);
		}
		return data_list;
	}
	
	//专业资料列表
	@At("/majorapi/?/?")
	@Ok("beetl:/include/pcenter/tab.html")
	public Object majorapi(String major_id , String project_id , HttpServletRequest request){
		NutMap nutMap = new NutMap("project_id",project_id);
		List <CommonTreeVO> mention_data_list = new LinkedList<>();
		List <DcProjectMentionInformation> mention_list = projectDesignManagerServiceImpl.getMentionDataListByMidAndPid(major_id, project_id);
		if(mention_list != null && !mention_list.isEmpty()){
			for(DcProjectMentionInformation dcProjectMentionInformation : mention_list){
				DesignMentionDataTreeVO mention_data = new DesignMentionDataTreeVO(dcProjectMentionInformation);
				mention_data_list.add(mention_data);
			}
		}
		nutMap.put("data", mention_data_list);
		return nutMap;
	}
	
	//动车机务设施相关
	@At("/maintenanceFacilities/?")
	@Ok("beetl:/include/pcenter/tab.html")
	public Object maintenanceFacilities(String project_id , HttpServletRequest request){
		NutMap nutMap = new NutMap("project_id",project_id);
		List <CommonTreeVO> mention_data_list = new LinkedList<>();
		List <DcProjectMentionInformation> mention_list = daoDcProjectMention.findDcjwssByProjectId(project_id);
		if(mention_list != null && !mention_list.isEmpty()){
			for(DcProjectMentionInformation dcProjectMentionInformation : mention_list){
				DesignMentionDataTreeVO mention_data = new DesignMentionDataTreeVO(dcProjectMentionInformation);
				mention_data_list.add(mention_data);
			}
		}
		nutMap.put("data", mention_data_list);
		return nutMap;
	}
	
	//动车设施相关
	@At("/trainfacilities/?")
	@Ok("beetl:/include/pcenter/tab.html")
	public Object majorapi(String project_id , HttpServletRequest request){
		NutMap nutMap = new NutMap("project_id",project_id);
		List <CommonTreeVO> mention_data_list = new LinkedList<>();
		List <DcProjectMentionInformation> mention_list = daoDcProjectMention.findSjdcssByProjectId(project_id);
		if(mention_list != null && !mention_list.isEmpty()){
			for(DcProjectMentionInformation dcProjectMentionInformation : mention_list){
				DesignMentionDataTreeVO mention_data = new DesignMentionDataTreeVO(dcProjectMentionInformation);
				mention_data_list.add(mention_data);
			}
		}
		nutMap.put("data", mention_data_list);
		return nutMap;
	}
	

}
